package com.seeyon.apps.nygj.manager.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.nygj.dao.SyncUnFlowDataDao;
import com.seeyon.apps.nygj.manager.SyncUnFlowDataManager;
import com.seeyon.apps.nygj.po.SyncUnFlowDataPO;
import com.seeyon.apps.nygj.util.FormCap4Kit;
import com.seeyon.apps.nygj.util.StrKit;
import com.seeyon.apps.nygj.util.SyncFiledKit;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.cap4.template.manager.CAPDataListSqlHelpManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import org.apache.commons.logging.Log;

import java.util.List;
import java.util.Map;

public class SyncUnFlowDataManagerImpl implements SyncUnFlowDataManager {

    private static final Log log = CtpLogFactory.getLog(SyncUnFlowDataManagerImpl.class);
    private CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");
    private CAPDataListSqlHelpManager capDataListSqlHelpManager = (CAPDataListSqlHelpManager) AppContext.getBean("capDataListSqlHelpManager");
    private SyncUnFlowDataDao syncUnFlowDataDao = (SyncUnFlowDataDao) AppContext.getBean("syncUnFlowDataDao");

    @Override
    public void saveSyncUnFlowDataPO(SyncUnFlowDataPO syncUnFlowDataPO) throws BusinessException {
        syncUnFlowDataDao.saveSyncUnFlowDataPO(syncUnFlowDataPO);
    }

    @Override
    public SyncUnFlowDataPO findSyncUnFlowDataPODataById(String state, String dataType) throws BusinessException {
        return syncUnFlowDataDao.findSyncUnFlowDataPODataById(state, dataType);
    }

    @Override
    public void insertOrUpdateMasterData(String dataType, JSONArray dataArray) throws BusinessException {
        try {
            FormBean formBean = cap4FormManager.getFormByFormCode("TZDATAWH_001");
            String sjlxFiledName = formBean.getFieldBeanByDisplay("数据类型").getName();
            String mbbmFiledName = formBean.getFieldBeanByDisplay("模版编码").getName();
            StringBuilder tzStb = new StringBuilder();
            tzStb.append("select id,").append(sjlxFiledName).append(",").append(mbbmFiledName);
            tzStb.append(" from ").append(formBean.getMasterTableBean().getTableName());
            tzStb.append(" where ").append(sjlxFiledName).append(" = '").append(dataType).append("'");
            List<Object> dataList = capDataListSqlHelpManager.selectDataList(tzStb.toString(), null);
            if (!dataList.isEmpty()) {
                for (int k = 0; k < dataList.size(); k++) {
                    Map<String, Object> dataMap = (Map<String, Object>) dataList.get(k);
                    String tzDataId = StrKit.str(dataMap.get("id"));
                    String templateCode = StrKit.str(dataMap.get(mbbmFiledName)).trim();
                    FormBean formBean_data = cap4FormManager.getFormByFormCode(templateCode);
                    for (int m = 0; m < dataArray.size(); m++) {
                        JSONObject dataObj = dataArray.getJSONObject(m);
                        if ("mdmPubVatRate".equals(dataType)) {
                            String name = StrKit.str(dataObj.get("name"));
                            if (name.contains("%")) {
                                String name_new = name.split("税")[1].replace("%", "");
                                double sl = Double.parseDouble(name_new) / 100;
                                dataObj.put("sl", sl);
                            }else if ("不固定税率销项税".equals(name)) {
                                dataObj.put("sl", 0);
                            }
                        }
                        FormDataMasterBean formDataMasterBean = null;
                        Map<String, List<Map<String, Object>>> fieldMap = SyncFiledKit.getSyncFields(tzDataId, dataObj, formBean, formBean_data);
                        String key = "";
                        List<Map<String, Object>> fieldList = null;
                        for (Map.Entry<String, List<Map<String, Object>>> entry : fieldMap.entrySet()) {
                            key = StrKit.str(entry.getKey());
                            fieldList = entry.getValue();
                        }
                        if ("new".equals(key)) {
                            formDataMasterBean = FormDataMasterBean.newInstance(formBean_data);
                        } else {
                            formDataMasterBean = cap4FormManager.getDataMasterBeanById(Long.parseLong(key), formBean_data, null);
                        }
                        for (int n = 0; n < fieldList.size(); n++) {
                            Map<String, Object> fieldMaps = fieldList.get(n);
                            for (Map.Entry<String, Object> entrys : fieldMaps.entrySet()) {
                                String fieldName = StrKit.str(formBean_data.getFieldBeanByDisplay(entrys.getKey().trim()).getName());
                                formDataMasterBean.addFieldValue(fieldName, StrKit.str(dataObj.get(entrys.getValue())));
                            }
                        }

                        FormCap4Kit.addUnFlowFormDataCap4(formDataMasterBean, formBean_data);
                    }
                }
            }


        } catch (Exception e) {
            log.error("档案底表同步数据失败:", e);
        }
    }
}
